package com.snake.springboot.custom.service.impl.fileoss;


import com.snake.springboot.custom.api.model.common.RedisKeyPrefix;
import com.snake.springboot.custom.api.model.fileoss.FileOSSModel;
import com.snake.springboot.custom.api.service.fileoss.FileOssService;
import com.snake.springboot.custom.dao.mybatis.entity.fileoss.FileOssDO;
import com.snake.springboot.custom.dao.mybatis.mappers.fileoss.FileOssMapper;
import com.snake.springboot.custom.service.assemble.fileoss.FileOssAssemble;
import org.snake.common.code.SerialNumberGeneratorFactory;
import org.snake.common.utils.JsonUtils;
import org.snake.common.utils.PropertyUtils;
import org.snake.redis.commons.RedisPlusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <p>文件OSS业务类
 * 类{@code FileOssServiceImpl} </br>
 * 创建时间2025/5/7 14:45
 * @author yangxt
 * @see
 */
@Service
public class FileOSSServiceImpl implements FileOssService {

    private FileOssMapper ossMapper;

    private RedisPlusUtils redisPlusUtils;
    @Autowired
    public FileOSSServiceImpl(FileOssMapper ossMapper, RedisPlusUtils redisPlusUtils) {
        this.ossMapper = ossMapper;
        this.redisPlusUtils = redisPlusUtils;
    }

    /**
     * 获取OSS配置
     * 1.查询存在的OSS配置
     * @param model
     * @return
     */
    @Override
    public FileOSSModel ossConfig(FileOSSModel model) {
        FileOssDO addDO = FileOssAssemble.toFileOssDO(model);
        FileOssDO fileOssDO = ossMapper.queryByCustomId(model.getCustomId());
        if (fileOssDO != null) {
            addDO.setFileOssId(fileOssDO.getFileOssId());
            ossMapper.update(addDO);
        }else {
            addDO.setFileOssId(SerialNumberGeneratorFactory.buildId4Bean());
            ossMapper.insertSelective(addDO);
        }
        model.setFileOssId(addDO.getFileOssId());

        String redisKey = RedisKeyPrefix.custom_oss_key + model.getCustomId();
        redisPlusUtils.delKey(redisKey);
        String redisKey1 = RedisKeyPrefix.oss_id_key + addDO.getFileOssId();
        redisPlusUtils.delKey(redisKey1);
        return model;
    }

    @Override
    public FileOSSModel ossQuery(FileOSSModel model) {
        FileOssDO fileOssDO = ossMapper.queryByCustomId(model.getCustomId());
        FileOSSModel ossModel = new FileOSSModel();
        if (fileOssDO!=null){
            PropertyUtils.copyProperties(ossModel,fileOssDO);
        }else {
            ossModel.setCustomId(model.getCustomId());
            ossModel.setRemark("请配置OSS");
        }
        return ossModel;
    }

    @Override
    public void ossDelete(FileOSSModel model) {
        FileOssDO ossDO = ossMapper.queryUsable(model.getCustomId());
        ossMapper.deleteByCustomId(model.getCustomId());
        String redisKey = RedisKeyPrefix.custom_oss_key + model.getCustomId();
        redisPlusUtils.delKey(redisKey);
        if (ossDO!=null){
            String redisKey1 = RedisKeyPrefix.oss_id_key + ossDO.getFileOssId();
            redisPlusUtils.delKey(redisKey1);
        }


    }

    @Override
    public FileOSSModel queryUsableCustomId(String customId) {
        String defVal="0";
        customId = customId == null ? defVal : customId;
        String redisKey = RedisKeyPrefix.custom_oss_key + customId;
        String opsVal = redisPlusUtils.getOpsVal(redisKey, String.class);
        if (opsVal == null) {
            FileOssDO ossDO = ossMapper.queryUsable(customId);
            if (ossDO == null) {
                redisPlusUtils.setOpsVal(redisKey, defVal, 20, TimeUnit.SECONDS);
                return null;
            }
            FileOSSModel model = new FileOSSModel();
            PropertyUtils.copyProperties(model,ossDO);
            redisPlusUtils.setOpsVal(redisKey, JsonUtils.toJson(model), 10, TimeUnit.MINUTES);
            return model;
        }else {
            if (!defVal.equals(opsVal)) {
                return JsonUtils.fromJson(opsVal, FileOSSModel.class);
            }
        }
        return null;
    }


    public FileOSSModel queryByOssId(String ossId){
        String defVal="0";
        ossId = ossId == null ? defVal : ossId;
        String redisKey = RedisKeyPrefix.oss_id_key + ossId;
        String opsVal = redisPlusUtils.getOpsVal(redisKey, String.class);
        if (opsVal == null) {
        if (defVal.equals(ossId)){
            FileOSSModel ossModel = queryUsableCustomId(null);
            if (ossModel == null) {
                redisPlusUtils.setOpsVal(redisKey, defVal, 10, TimeUnit.SECONDS);
            }else {
                redisPlusUtils.setOpsVal(redisKey, JsonUtils.toJson(ossModel), 10, TimeUnit.MINUTES);
            }
            return ossModel;
        }
        FileOssDO ossDO = ossMapper.queryByOssId(ossId);
        if (ossDO == null) {
            redisPlusUtils.setOpsVal(redisKey, "0", 10, TimeUnit.SECONDS);
            return null;
        }
        FileOSSModel model = new FileOSSModel();
        PropertyUtils.copyProperties(model,ossDO);
        redisPlusUtils.setOpsVal(redisKey, JsonUtils.toJson(model), 10, TimeUnit.MINUTES);
        return model;
        }else {
            if (!defVal.equals(opsVal)) {
                return JsonUtils.fromJson(opsVal, FileOSSModel.class);
            }
            return null;
        }
    }




}
